﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using ArkEC.CoreCom.Datastruct.WebHandler;
using System.Reflection;
using ArkEC.CoreCom.CacheStore;
using ArkEC.CoreCom.Datastruct;
using System.Data;
using System.Collections;
using ArkEC.CoreCom.Runtime;

namespace ArkEC.CoreCom.WebHandler
{
    /// <summary>
    /// 业务请求处理
    /// </summary>
    public class BizHandler
    {
        /// <summary>
        /// 执行请求处理
        /// </summary>
        /// <param name="context"></param>
        public static string HandlerRequest(HttpContext context)
        {
            string responseStr = "";

            //获取请求参数
            string param = context.Request.Form["Param"];

            //JavaScript和服务器的数据传输协议的转换
            WebTransferObj transferObj = ObjectConvertion.JsonToObject<WebTransferObj>(param);

            //获取具体执行动作
            BizActions.Item actionItem = CacheHelper.GetItem(BizActions.Instance, transferObj.Action) as BizActions.Item;

            //执行参数的转换
            ArrayList actionParams = new ArrayList();
            if (actionItem.ParamType == null)
            {
                if (transferObj.BizObj != null)
                {
                    actionParams.Add(ObjectConvertion.JsonToObject<Dictionary<string, string>>(transferObj.BizObj));
                }
            }
            else
            {
                actionParams.Add(ObjectConvertion.JsonToObject(actionItem.ParamType, transferObj.BizObj));
            }

            //上传文件处理
            if (context.Request.Files.Count > 0)
            {
                HttpPostedFile file = context.Request.Files[context.Request.QueryString["Filename"]];
                string filePath = AppDomain.CurrentDomain.BaseDirectory + RunSetting.TempFilePath + Guid.NewGuid().ToString() + "-" + file.FileName;
                file.SaveAs(filePath);

                actionParams.Add(filePath);
            }

            object returnObj = null;
            if (actionParams.Count > 0)
            {
                returnObj = actionItem.Method.Invoke(null, actionParams.ToArray());
            }
            else
            {
                returnObj = actionItem.Method.Invoke(null, null);
            }

            if (returnObj != null)
            {
                //标准数据表
                if (returnObj.GetType() == typeof(DataTable))
                {
                    AjaxTableStruct tableStruct = new AjaxTableStruct(returnObj as DataTable);
                    tableStruct.TotalCount = tableStruct.Rows.Count;
                    returnObj = tableStruct;
                }
                //分页数据表
                else if (returnObj.GetType() == typeof(PagingQueryStruct))
                {
                    PagingQueryStruct pagingStruct = returnObj as PagingQueryStruct;
                    AjaxTableStruct tableStruct = new AjaxTableStruct(pagingStruct.PagingTable);
                    tableStruct.TotalCount = pagingStruct.TotalCount;
                    returnObj = tableStruct;
                }
            }

            responseStr = ObjectConvertion.ObjectToJson(returnObj);

            /* 文件上传动作 */
            if (context.Request.Files.Count > 0)
            {
                context.Response.Cookies.Add(new HttpCookie("Response", responseStr));
            }

            return responseStr;
        }
    }
}
